package com.panshi.service.impl;

import com.panshi.api.util.PageSizeVO;
import com.panshi.bean.*;
import com.panshi.config.EmailPhoneUtill;
import com.panshi.mapper.IssMapper;
import com.panshi.service.IssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.List;

@Service
public class IssServiceImpl implements IssService {
    @Resource
    private IssMapper issMapper;

    @Autowired
    private EmailPhoneUtill emailPhoneUtill;


    @Cacheable(value = "issue",key="#p0.startIndex+#root.methodName+#p0.answer+#p0.tid")//hasbei:Issue:key-->数据存入redis的客户端中的hasbei下的Issue下的key(#root.methodName方法名)
    public List<IssBean> findAllIss(PageUtil page) {//查询所有问题(全部,主题,已回答未回答)
        return issMapper.findAllIss(page);
    }

    @Cacheable(value = "issue",key="#p0.startIndex+#root.methodName")
    public List<IssBean> findAllIss2(PageUtil page) {//查询所有问题(全部,主题,已回答未回答)====搜索的热榜
        return issMapper.findAllIss2(page);
    }
    @Cacheable(value = "issue",key="#p0.startIndex+#root.methodName")
    public List<IssBean> findAllIss3(PageUtil page) {//查询所有问题(全部,主题,已回答未回答)=====搜索的最新
        return issMapper.findAllIss3(page);
    }

    @Cacheable(value = "issue",key="#root.methodName")
    public List<IssBean> findAllTid() {//查询所有主题
        return issMapper.findAllTid();
    }

    @CacheEvict(value="issue",allEntries=true)
    public int initIss(IssBean iss) {//用户提出问题存入未审核表
        System.out.println(iss+"==================");
        issMapper.initIss(iss);
        return iss.getQid_no();
    }
    public int initIssImg(ImageBean img) {//用户提出问题的图片存入图片表
        return issMapper.initIssImg(img);
    }

    //删除问题===========================================================================================================
    public int deleteIssImg(int qid,int state) {//先删除问题的图片(问题审核表和问题表)
        return issMapper.deleteIssImg(qid,state);
    }
    public List<Integer> getByQidFindAns(int qid) {//查该问题下有多少个回答,一一删除
        return issMapper.getByQidFindAns(qid);
    }
    public int deleteQaAnswer(int qid) {//删除该问题下所有回答
        return issMapper.deleteQaAnswer(qid);
    }
    public List<Integer> getByQidFindAnsNo(int qid) {//查该问题下有多少个回答,一一删除(回答审核表)
        return issMapper.getByQidFindAnsNo(qid);
    }
    public int deleteQaAnswerNo(int qid) {//删除该问题下所有回答(回答审核表)
        return issMapper.deleteQaAnswerNo(qid);
    }
    public int deleteQaAnsCom(int qid) {//删除问题里回答的评论
        return issMapper.deleteQaAnsCom(qid);
    }
    public int deleteQaAnsLike(int qid) {//删除问题里回答的点赞
        return issMapper.deleteQaAnsLike(qid);
    }
    @CacheEvict(value="issue",allEntries=true)
    public int deleteIss(int qid) {//用户问题表删除自己提出的问题(判断去审核表删还是问题表删)
        return issMapper.deleteIss(qid);
    }
    public int deleteUserQaCount(int uid) {//给用户提问数-1
        return issMapper.deleteUserQaCount(uid);
    }

    @CacheEvict(value="issue",allEntries=true)
    public int deleteIssNo(int qid) {//用户审核表删除自己提出的问题(判断去审核表删还是问题表删)
        return issMapper.deleteIssNo(qid);
    }


    //点击问题进行阅读=====================================================================================================
    public List<AnswerBean> getByQidAnswer(int qid) {//查询该问题下的所有回答
        return issMapper.getByQidAnswer(qid);
    }
    @Cacheable(value = "issue",key="#root.methodName+#qid")
    public IssBean getByQid(int qid) {//用户点击某个个问题根据问题id查询数据
        return issMapper.getByQid(qid);
    }
    public int addReadCount(int qid) {//问题表阅读数加1
        return issMapper.addReadCount(qid);
    }


    //用户收藏问题========================================================================================================
    public int initColl(int uid,int qid) {//用户点击收藏
        return issMapper.initColl(uid,qid);
    }
    public IssCollBean findColl(int uid, int qid) {//判断用户在某问题是否已点赞收藏
        return issMapper.findColl(uid,qid);
    }
    public int updateQa(int qid) {//修改问题表的收藏量
        return issMapper.updateQa(qid);
    }
    public int findCollCount(int qid) {//重新查询收藏数返给前台
        return issMapper.findCollCount(qid);
    }

    //周边小功能==========================================================================================================
    @Cacheable(value = "issue",key="#i+#root.methodName+#tid")
    public List<IssBean> findHostIss(int tid,int i) {//大家都在问
        return issMapper.findHostIss(tid,i);
    }
    @Cacheable(value = "issue",key="#root.methodName+#tid")
    public List<IssBean> aboutQa(int tid) {//点击问题后右边的相关推荐
        return issMapper.aboutQa(tid);
    }

    //栏目功能============================================================================================================
    @Cacheable(value = "issue",key="#root.methodName")
    public List<ColumnBean> findColumn() {//查询栏目信息
        return issMapper.findColumn();
    }
    public int userMessage(UserMessageBean um) {//用户留言
        return issMapper.userMessage(um);
    }







    //管理员接口==========================================================================================================
    @Cacheable(value = "issue",key="#p0.startIndex+#root.methodName+#p0.state")
    public List<IssBean> adminFindAll(PageSizeVO pageSizeVO) {//管理员查询所有问题
        return issMapper.adminFindAll(pageSizeVO);
    }

    @Cacheable(value = "issue",key="#p0.startIndex+#root.methodName+#p0.state")
    public List<IssBean> adminFindNoAll(PageSizeVO pageSizeVO) {//管理员问题审核表所有问题
        return issMapper.adminFindNoAll(pageSizeVO);
    }

    public int adminFindAllSum(int state) {//管理员查询问题表的总记录数
        return issMapper.adminFindAllSum(state);
    }
    public int adminFindNoAllSum(int state) {//管理员问题审核表总记录数
        return issMapper.adminFindNoAllSum(state);
    }

    @CacheEvict(value="issue",allEntries=true)
    public int initQa(IssBean iss) {//管理员审核通过后进入问题表
        issMapper.initQa(iss);
        return iss.getQid();
    }

    @CacheEvict(value="issue",allEntries=true)
    public int adminUpdateIssNo(IssBean iss) {//管理员修改审核表
        return issMapper.adminUpdateIssNo(iss);
    }

    public int addQaSearch(IssBean iss) {//审核通过将问题存入搜索表(先查问题的主键)
        return issMapper.addQaSearch(iss);
    }
    public int addQaInstation(IssBean iss) {//审核后将问题存入站内消息表（i：0通过 1未通过）
        return issMapper.addQaInstation(iss);
    }
    public int addDynamic(IssBean iss) {//将问题存入动态表
        return issMapper.addDynamic(iss);
    }
    public int addUserQaCount(int uid) {//审核通过给用户提问总数+1
        return issMapper.addUserQaCount(uid);
    }

    @CacheEvict(value="issue",allEntries=true)
    public int adminUpdateQa(int qid,int state,int tid) {//管理员修改审核表
        return issMapper.adminUpdateQa(qid,state,tid);
    }
    public int findSearchByAid(int qid) {//查询搜索表数据是否已存在
        return issMapper.findSearchByAid(qid);
    }
    public int findDynamicByQid(int qid) {//查询动态表数据是否已存在
        return issMapper.findDynamicByQid(qid);
    }

    public int deleteQaDynamic(int qid) {//下架时删除动态表数据
        return issMapper.deleteQaDynamic(qid);
    }
    public int deleteQaSearch(int qid) {//删除搜索表
        return issMapper.deleteQaSearch(qid);
    }

    @CacheEvict(value="issue",allEntries=true)
    public int updateColumn(ColumnBean cb) {//管理员修改栏目
        return issMapper.updateColumn(cb);
    }

    @CacheEvict(value="issue",allEntries=true)
    public int initColumn(ColumnBean cb) {//管理员增加栏目
        return issMapper.initColumn(cb);
    }








    //审核通过给用户发送邮箱问题提醒=========================================================================================
    @Override
    public void setemailquestion(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = issMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==1){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网发布的问题:</p>《"+title+"》<p>审核通过 已上架</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网发布的文章:</p>《"+title+"》<p>内容违反国家法律规定 已被强制下架 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
        System.out.println(message+"=========");
    }

    @Override
    public void setemailquestionno(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = issMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网提出的问题:</p>《"+title+"》<p>违反国家法律规定 审核不通过 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==3){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网提出的问题:</p>《"+title+"》<p>审核通过</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
        System.out.println(message+"=========");
    }

}